#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Des.h"
#include<QMessageBox>
#include<QString>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 设置初值
    ui->input->setText(tr("000000000000499910DBFF020006694279E700CB3FE04F86F0241625"));
    ui->key->setText(tr("404142434445464748494A4B4C4D4E4F"));
    // 首次调用
    on_input_textChanged("");
    on_key_textChanged("");
}

MainWindow::~MainWindow()
{
    delete ui;
}

std::string MainWindow::generator(const std::string &key, const std::string &data)
{
    CDes des;
    std::string bsHostRnd = "D4DCA09B01A881B5";
    std::string bsResp=data;
    std::string bsCardRnd = bsResp.substr(12 * 2, 8 * 2);
    std::string bsCardInx = bsCardRnd.substr(0 * 2, 2 * 2);
    std::string KDCenc =key;// "404142434445464748494A4B4C4D4E4F";
    std::string KDCmac =key;// "404142434445464748494A4B4C4D4E4F";
    std::string bsDiv2 = "0182" + bsCardInx + "000000000000000000000000";
    std::string bsDiv4 = "0101" + bsCardInx + "000000000000000000000000";
    std::string KSCenc;
    std::string KSCmac;
    des.fnb3Des_CBC(KDCenc, bsDiv2, KSCenc);
    des.fnb3Des_CBC(KDCmac, bsDiv4, KSCmac);
    std::string bsRnd = bsCardRnd + bsHostRnd + "80";
    bsRnd.append(16 - bsRnd.size() % 16, '0');
    std::string bsEncCardRnd;
    des.fnb3Des_CBC(KSCenc, bsRnd, bsEncCardRnd);
    bsEncCardRnd = bsEncCardRnd.substr(bsEncCardRnd.size() - 16, 16);
    std::string bsEAApdu = "8482000010" + bsEncCardRnd;
    std::string bsCMAC;
    des.fnbMAC_3DES_FT(KSCmac, "0000000000000000", bsEAApdu, bsCMAC);
    std::string result="8482000010"+bsEncCardRnd+bsCMAC;
    // 设置全部大写
    for(size_t i=0;i<result.size();++i){
        if(result[i]>='a'&&result[i]<='z'){
            result[i]=result[i]-'a'+'A';
        }
    }
    return result;
}

void MainWindow::on_generator_clicked()
{
    std::string data=ui->input->text().toStdString();
    if(data.size()!=56){
        QMessageBox::information(this,tr("Warning"),tr("The input's length is not enough 56"));
        return;
    }
    std::string key=ui->key->text().toStdString();
    if(key.size()!=32){
        QMessageBox::information(this,tr("Warning"),tr("The key's length is not enough 16"));
        return;
    }
    std::string result=generator(key,data);
    ui->out->setText(QString::fromStdString(result));
}

void MainWindow::on_input_textChanged(const QString &)
{
    QString input=ui->input->text();
    QChar last=input.right(1)[0];
    // 判断末端的字符
    if(last>='0'&&last<='9'){
    }else if(last>='A'&&last<='F'){
    }else if(last>='a'&&last<='f'){
        ui->input->setText(input.toUpper());
    }else{
        ui->input->setText(input.left(input.length()-1));
    }
    // 将长度限制在56
    input=ui->input->text();
    input=input.left(std::min(56,input.length()));
    // 更新输入框和长度框
    ui->input->setText(input);
    ui->inputLen->setText(QString::number(ui->input->text().length()));
}

void MainWindow::on_key_textChanged(const QString &)
{
    QString input=ui->key->text();
    QChar last=input.right(1)[0];
    // 判断末端的字符
    if(last>='0'&&last<='9'){
    }else if(last>='A'&&last<='F'){
    }else if(last>='a'&&last<='f'){
        ui->key->setText(input.toUpper());
    }else{
        ui->key->setText(input.left(input.length()-1));
    }
    // 将长度限制在32
    input=ui->key->text();
    input=input.left(std::min(32,input.length()));
    // 更新输入框和长度框
    ui->key->setText(input);
    ui->keyLen->setText(QString::number(ui->key->text().length()));
}
